import isEmpty from 'lodash/isEmpty';
import { WeaSearchGroup, WeaFormItem, WeaTools } from 'ecCom';
import { WeaSwitch } from 'comsMobx';

export const getNewFiels = (fields = {}) => {
  let params = {};
  if (!isEmpty(fields)) {
    for (let key in fields) {
      params[fields[key].name] = fields[key].value;
    }
  }
  return params;
};

// 用于获取高级搜索中的表单控件
// 一定要先初始化initFormFields表单对象
export const getSearchs = (form, conditioninfo, onEnterSearch) => {
  const { isFormInit } = form;
  let group = [];
  if (isFormInit) {
    // isFormInit 为true，form已经初始化完成
    isFormInit &&
      conditioninfo.map((c) => {
        let items = [];
        c.items.map((fields) => {
          items.push({
            com: (
              <WeaFormItem
                ecId={`${
                  (this && this.props && this.props.ecId) || ''
                }_WeaFormItem@99smke@${fields.domkey[0]}`}
                label={`${fields.label}`}
                labelCol={{ span: `${fields.labelcol}` }}
                wrapperCol={{ span: `${fields.fieldcol}` }}
              >
                {/* 根据 WeaSwitch 获取对应的组件 */}
                <WeaSwitch
                  ecId={`${
                    (this && this.props && this.props.ecId) || ''
                  }_WeaSwitch@a9gjdv@${fields.domkey[0]}`}
                  fieldConfig={fields}
                  form={form}
                  onEnterSearch={() => {
                    if (onEnterSearch && typeof onEnterSearch === 'function') {
                      onEnterSearch();
                    }
                  }}
                />
              </WeaFormItem>
            ),
            colSpan: 1,
          });
        });
        group.push(
          <WeaSearchGroup
            ecId={`${
              (this && this.props && this.props.ecId) || ''
            }_WeaSearchGroup@98fhko@form`}
            needTigger={true}
            title={c.title}
            showGroup={c.defaultshow}
            items={items}
          />,
        );
      });
  }
  return group;
};

export function throttle(fn, isArrowFn = false, interval) {
  let __self = fn, // 保存需要被延迟执行的函数引用
    timer, // 定时器
    firstTime = true; // 是否是第一次调用
  return function (...args) {
    let __me = isArrowFn ? null : this;
    if (firstTime) {
      // 如果是第一次调用，不需延迟执行
      __self.apply(__me, args);
      return (firstTime = false);
    }
    if (timer) {
      // 如果定时器还在，说明前一次延迟执行还没有完成
      return false;
    }

    timer = setTimeout(() => {
      // 延迟一段时间执行
      clearTimeout(timer);

      timer = null;
      !isArrowFn ? __self.apply(__me, args) : __self(args);
    }, interval || 500);
  };
}

// 静态刷新邮件菜单收件箱数量
export const updateMenuCount = async (params = {}, refreshType = '') => {
  let waitdealCountDom = document.getElementById('waitDealCount_id');
  let inboxCountDom = document.getElementById('unreadMailCount_id');
  let flag = true;
  const emailInterface = () =>
    WeaTools.callApi('/api/portal/emailmenu/unreadCount', 'GET', params);

  if (refreshType) {
    if (![`${refreshType}CountDom`]) {
      flag = false;
      return false;
    }
    const res = await emailInterface();
    [`${refreshType}CountDom`].innerHTML = res[`${refreshType}Count`];
  } else {
    const { inboxCount, waitdealCount } = await emailInterface();
    // 收件箱
    if (inboxCountDom) {
      inboxCountDom.innerHTML = inboxCount;
    }
    // 待办
    if (waitdealCountDom) {
      waitdealCountDom.innerHTML = waitdealCount;
    }
  }
  return flag;
};
